package com.corpgovernment.common.logging.service.impl;

import org.slf4j.Logger;

import com.ctrip.corp.obt.generic.utils.JsonUtils;
import com.ctrip.corp.obt.logging.marker.TagMarker;

/**
 * @author: pwang27
 * @Date: 2023/11/8 17:34
 * @Description:
 */
public class LoggerMessage {

    private final TagMarker marker;

    private final Object message;

    private final Throwable exception;

    private final Logger logger;

    public LoggerMessage(Logger logger,
            TagMarker marker,
            Object message,
            Throwable exception) {
        this.marker = marker;
        this.logger = logger;
        this.message = message;
        this.exception = exception;
    }

    private String buildMessage() {
        return this.message instanceof String ? (String)message : JsonUtils.toJsonString(message);
    }

    private TagMarker boxingMarker() {
        return this.marker;
    }

    public void info() {
        try {
            if (this.exception == null) {
                this.logger.info(this.marker, this.buildMessage());
            } else {
                this.logger.info(this.marker, this.buildMessage(), this.exception);
            }
        } catch (Exception ignore) {
        }
    }

    public void warn() {
        try {
            if (this.exception == null) {
                this.logger.warn(this.marker, this.buildMessage());
            } else {
                this.logger.warn(this.marker, this.buildMessage(), this.exception);
            }
        } catch (Exception ignore) {
        }
    }

    public void error() {
        try {
            if (this.exception == null) {
                this.logger.error(this.boxingMarker(), this.buildMessage());
            } else {
                this.logger.error(this.boxingMarker(), this.buildMessage(), this.exception);
            }
        } catch (Exception ignore) {
        }
    }

}
